package com.TechM.get;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.TechM.get.AppTask.CurrentData;
import com.TechM.get.AppTask.ServiceInfo;

public class GetData {
	public Map<String, Object> query(String deviceId){
    	String strIP = "218.4.33.72";
		String strPort = "8743";
		String strAppId = "cc586dc5-8798-4212-84fb-752dc19cb993";
		String strPassword = "a3bbd98cd25b8218310d";
		try {
			//登录
			AppTask at = new AppTask(strIP, strPort, strAppId, strPassword);
			boolean b=at.authentication();
			//登陆成功后开始查询数据
			if(b){
				/*System.out.println("---------------LoginSuccess");
				//获取diviceID
				Vector<String> vector = at.getDeviceList("All","All","");
				String listV = "DeviceID：";
				for (String v : vector) {					
					listV = listV+v+";";
				}	
				System.out.println(listV);
				
				//获取serviceID
				String deviceID = "14dec7f6-76cd-4f05-8bba-6b0710c0c345";
				Vector<ServiceInfo> vSInfos = at.getServiceList(deviceID);
				String listVsInfos = "ServiceID:";
				for (ServiceInfo vSInfosSigle : vSInfos) {
					listVsInfos = listVsInfos+vSInfosSigle.mStrServiceId+"--";
				}
				System.out.println(listVsInfos);*/
				
				//获取data
				CurrentData Ctrip = at.queryData(deviceId,"Trip");
				CurrentData CtimeStamp = at.queryData(deviceId,"TimeStamp");
				CurrentData Clocation = at.queryData(deviceId,"Location");
				CurrentData Cmileage = at.queryData(deviceId,"Mileage");
				CurrentData Cengine = at.queryData(deviceId,"Engine");
				CurrentData Cdrivier = at.queryData(deviceId,"DriverBehavior");
				/*
				Map<String, Object> ct= new HashMap<String, Object>();
				Map<String, Object> cts= new HashMap<String, Object>();
				Map<String, Object> cl= new HashMap<String, Object>();
				Map<String, Object> cm= new HashMap<String, Object>();
				Map<String, Object> ce= new HashMap<String, Object>();
				Map<String, Object> cd= new HashMap<String, Object>();*/
				
				
				Map<String, Object> iData = new HashMap<String, Object>();//存储每个serviceID下的数据
				List<CurrentData> allData = new ArrayList<CurrentData>();//存储所有service查出来的数据
				allData.add(Ctrip);
				allData.add(CtimeStamp);
				allData.add(Clocation);
				allData.add(Cmileage);
				allData.add(Cengine);
				allData.add(Cdrivier);
				Map<String, Object> map = new HashMap<String, Object>();//存储最终查询出的数据				
				
				/*List<Map> listMap = new ArrayList<>();
				listMap.add(ct);
				listMap.add(cts);
				listMap.add(cl);
				listMap.add(cm);
				listMap.add(ce);
				listMap.add(cd);*/
				for (CurrentData sigleData : allData) {
						if(sigleData==null){
							iData = null; 
						}else {
							iData = null;
							iData = sigleData.mMapData; //把数据放到map类型的iData中
							//遍历数据并放到map
							for (Map.Entry<String,Object> entry:iData.entrySet()) {
								map.put(entry.getKey(), entry.getValue());
							}
						}
				}
				/*
				//Trip
				if(Ctrip==null){
					ct = null;
				}else {
					ct = Ctrip.mMapData;
					//将遍历到的数据加到map
					for (Map.Entry<String,Object> entry:ct.entrySet()) {
						map.put(entry.getKey(), entry.getValue());
					}
					System.out.println("ct----"+map);
				}
				//TimeStamp
				if(CtimeStamp==null){
					cts = null;
				}else {
					cts = CtimeStamp.mMapData;
					for (Map.Entry<String,Object> entry:cts.entrySet()) {
						map.put(entry.getKey(), entry.getValue());
					}
					System.out.println("cts----"+map);
				}
				//Location
				if(Clocation==null){
					cl = null;
				}else {
					cl = Clocation.mMapData;
					for (Map.Entry<String,Object> entry:cl.entrySet()) {
						map.put(entry.getKey(), entry.getValue());
					}
					System.out.println("cl----"+map);
				}
				//Mileage
				if(Cmileage==null){
					cm = null;
				}else {
					cm = Cmileage.mMapData;
					for (Map.Entry<String,Object> entry:cm.entrySet()) {
						map.put(entry.getKey(), entry.getValue());
					}
					System.out.println("cm----"+map);
				}
				//Engine
				if(Cengine==null){
					ce = null;
				}else {
					ce = Cengine.mMapData;
					for (Map.Entry<String,Object> entry:ce.entrySet()) {
						map.put(entry.getKey(), entry.getValue());
					}
					System.out.println("ce----"+map);
				}
				//DriverBehavior
				if(Cdrivier==null){
					cd = null;
				}else {
					cd = Cdrivier.mMapData;
					for (Map.Entry<String,Object> entry:cd.entrySet()) {
						map.put(entry.getKey(), entry.getValue());
					}
				}*/
				map.put("deviceId",deviceId);
				
				
//				System.out.println("test------"+map);
				
				
				return map;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }
}
